﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit.Electric.Common;

namespace Model
{
	internal class SelectionTypeUtility
	{
		public SelectionTypeUtility(UIApplication uiapplication)
		{
			this.uiApp = uiapplication;
			this.doc = this.uiApp.ActiveUIDocument.Document;
			this.selection = this.uiApp.ActiveUIDocument.Selection;
		}

		public void ShowMainDlg()
		{
			try
			{
				if (this.slotDirection == null)
				{
					this.slotDirection = new DlgVerticalBridgeSlotDirection();
				}
				DialogResult dialogResult = this.slotDirection.ShowDialog();
				if (dialogResult != DialogResult.OK)
				{
					if (dialogResult == DialogResult.Cancel)
					{
						return;
					}
				}
				else if (this.slotDirection.SelectionMethod == 0)
				{
					this.SelectSlotDirection();
				}
				else if (this.slotDirection.SelectionMethod == 1)
				{
					this.RotateVerticalTray();
				}
				this.ShowMainDlg();
			}
			catch (Exception)
			{
			}
		}

		private void SelectSlotDirection()
		{
			try
			{
				XYZ xyz = this.selection.PickPoint("请选择桥架线槽方向第一点");
				XYZ xyz2 = this.selection.PickPoint("请选择桥架线槽方向第二点,用此两点构成的方向与X轴的角度");
				this.selSlotDirection = (xyz2 - xyz).Normalize();
				double num = XYZ.BasisX.AngleOnPlaneTo(this.selSlotDirection, XYZ.BasisZ);
				this.slotDirection.hytxtAngle.Text = (num / 0.017453292519943295).ToString("0");
			}
			catch (Exception)
			{
			}
		}

		private void RotateVerticalTray()
		{
			try
			{
				for (;;)
				{
					Reference reference = this.selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new RevitFilter.SelectionVerticalCableTrayFilter(), "请选择需要调整线槽方向的桥架");
					HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(this.doc.GetElement(reference) as CableTray);
					XYZ xyz = -hycableTray.CustomConnectorStart.Connector.CoordinateSystem.BasisY;
					double num;
					if (this.slotDirection.rdoRight.Checked)
					{
						num = xyz.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ);
					}
					else if (this.slotDirection.rdoBehind.Checked)
					{
						num = xyz.AngleOnPlaneTo(XYZ.BasisY, XYZ.BasisZ);
					}
					else if (this.slotDirection.rdoLeft.Checked)
					{
						num = xyz.AngleOnPlaneTo(-XYZ.BasisX, XYZ.BasisZ);
					}
					else if (this.slotDirection.rdoFront.Checked)
					{
						num = xyz.AngleOnPlaneTo(-XYZ.BasisY, XYZ.BasisZ);
					}
					else
					{
						double num2 = xyz.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ);
						num = Convert.ToDouble(this.slotDirection.hytxtAngle.Text.Trim());
						num = num * Math.PI / 180.0 + num2;
					}
					Transaction transaction = new Transaction(this.doc, "SlotDirection");
					transaction.Start();
					this.RotateTray(hycableTray, num);
					this.doc.Regenerate();
					transaction.Commit();
				}
			}
			catch (Exception)
			{
			}
		}

		private void RotateTray(HYCableTray<CableTray> hyCableTray, double angle)
		{
			Transform coordinateSystem = hyCableTray.CustomConnectorStart.Connector.CoordinateSystem;
			Line line = Line.CreateUnbound(hyCableTray.StartXYZ, XYZ.BasisZ);
			hyCableTray.CableTray.Location.Rotate(line, angle);
		}

		private UIApplication uiApp;

		private Document doc;

		private Selection selection;

		private XYZ selSlotDirection;

		private DlgVerticalBridgeSlotDirection slotDirection;
	}
}
